/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
package awd.bj.jls.webs.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import awd.bj.jls.model.QjcsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.jls.model.ClcsModel;
import awd.bj.jls.model.ShgxModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.bj.jls.webs.utils.TermType;

@Controller
@RequestMapping("/qjcs")
@RefreshScope
public class QjcsAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/qjcs.html" //首页
            ,"/qjcs_enter.html" //单表录入页面
            ,"/qjcs_jbxx_enter.html" //人员信息查询页面
            ,"/qjcs_query.html" //信息查询
            ,"/qjcs_jbxx_query.html" //关联基本信息查询
            ,"/6.html" //
            ,"/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }


    /**
     * 数据保存操作
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result<?> save(@ModelAttribute QjcsModel model, ShgxModel model2){
        User users = getCurrentUser();
        System.out.println("\33[34;4m张天啸，请假出所-请假呈批：接收前台的参数(clcs)"+ JSON.toJSONString(model)+"\33[30;0m");
        System.out.println("\33[34;4m张天啸，请假出所-请假呈批：接收前台的参数(shgx)"+ JSON.toJSONString(model2)+"\33[30;0m");
        try {
            model.setState("R2");
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());

            model2.setState("R2");
            model2.setJsbh(users.getJsbh());
            model2.setCreatetime(new Date());
            model2.setCreator(users.getUsername());
            model2.setUpdator(users.getUsername());
            model2.setUpdatetime(new Date());

            System.out.println("\33[32;4m张天啸，请假出所-请假呈批：传往后台的参数"+ JSON.toJSONString(model)+"\33[30;0m");
            jlsServersApi.qjcsSave(model);
            jlsServersApi.shgxSave(model2);
            return ResultUtils.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }



    /**
     * 数据列表
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request,Map<String,Object> params) {
        QueryParam param = getBaseQueryParam(request,params);

        param.and("ksrqStart",TermType.eq,request.getParameter("ksrqStart"));
        param.and("ksrqEnd",TermType.eq,request.getParameter("ksrqEnd"));
        param.and("dbr",TermType.eq,request.getParameter("dbr"));
        param.and("gjmj",TermType.eq,request.getParameter("gjmj"));
        param.and("gjqmsjStart",TermType.eq,request.getParameter("gjqmsjStart"));
        param.and("gjqmsjEnd",TermType.eq,request.getParameter("gjqmsjEnd"));
        System.err.println("张天啸:请假出所请求参数========================================================");
        System.err.println(JSON.toJSONString(param));
        System.err.println("=======================================================================");
        ResponseMessage<PagerResult<ClcsModel>> list = jlsServersApi.clcsQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }


}